﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Contacts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadLogin();
            loadGroup();
        }
    }

   
    private void loadLogin()
    {
        if (hfLoginID.Value == "")
        {
            hfLoginID.Value = getLogin().ToString();
        }
    }

    private string getLoginID()
    {
        return hfLoginID.Value.Substring(6, hfLoginID.Value.Length - 6).Substring(0, hfLoginID.Value.Length - 12);


    }
    private string getLogin()
    {
        string loginID="";
        try
        {
            if (Session["OnlineSMS_LoginID"] != null)
            {
                loginID = Session["OnlineSMS_LoginID"].ToString();
            }
            else if (hfLoginID.Value != "")
            {
                loginID = hfLoginID.Value;
            }
            else
            { 
                Session["PreviousPage"] = HttpContext.Current.Request.Url.AbsoluteUri; Response.Redirect("Default.aspx"); }

        }
        catch (Exception ex)
        { }

        return loginID;
    }
   
    private void loadGroup()
    {
        string sql = @"
SELECT  [GroupID]
      ,[GroupName]
      ,[dbo].[GetContactCountyLoginID](GroupID) as NoOfContacts
  FROM [Group] where StatusID=1 and LoginID=" + getLoginID() + @"
order by GroupName
";

        DataSet ds = DatabaseManager.ExecSQL(sql);

        ddlGroups.Items.Clear();
        ddlGroupShow.Items.Clear();
        ddlFileContactGroup.Items.Clear();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            ddlGroups.Items.Add(new ListItem(dr["GroupName"].ToString(), dr["GroupID"].ToString()));
            ddlGroupShow.Items.Add(new ListItem(dr["GroupName"].ToString(), dr["GroupID"].ToString()));
            ddlFileContactGroup.Items.Add(new ListItem(dr["GroupName"].ToString(), dr["GroupID"].ToString()));
        }

    }

    protected void lbDeleteGroup_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        DatabaseManager.ExecSQL("Update [Group] set StatusID=0 where GroupID=" + linkButton.CommandArgument);
        loadGroup();
    }
    protected void lbDeleteContact_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        DatabaseManager.ExecSQL("Delete [Contact] where ContactID=" + linkButton.CommandArgument);
        loadGroup();
        LoadShowContact();
    }


    protected void btnSaveContact_Click(object sender, EventArgs e)
    {
        string sql = @"
    INSERT INTO [Contact]
           ([Name]
           ,[ShortName]
           ,[MobileNo]
           ,[GroupID])
     VALUES
           ('"+txtContactName.Text+@"'
           ,'" + txtShortName.Text + @"'
           ,'" + txtMobile.Text + @"'
           ," +ddlGroups.SelectedValue+@");
";
        DatabaseManager.ExecSQL(sql);
        LoadShowContact();
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        LoadShowContact();
    }

    private void LoadShowContact()
    {
        string sql = @"
SELECT [ContactID]
      ,[Name]
      ,[ShortName]
      ,[MobileNo]
      ,[GroupID]
  FROM [Contact] where GroupID="+ddlGroupShow.SelectedValue+@"
";

        DataSet ds = DatabaseManager.ExecSQL(sql);

        gvContact.DataSource = ds.Tables[0];
        gvContact.DataBind();
    }
    protected void btnFileUpload_Click(object sender, EventArgs e)
    {
        string fileNmae = uplaodFile();
        string[] lines = System.IO.File.ReadAllLines(MapPath("tmp/" + "/" + fileNmae));
        string sql = "";
        int wrongNO = 0;
        foreach (string line in lines)
        {
            if (processMobileNO(line) != "")
            {
                sql = @"
                            INSERT INTO [Contact]
                                   ([Name]
                                   ,[ShortName]
                                   ,[MobileNo]
                                   ,[GroupID])
                             VALUES
                                   (''
                                   ,''
                                   ,'" + processMobileNO(line) + @"'
                                   ," + ddlFileContactGroup.SelectedValue + @");
                        ";
                DatabaseManager.ExecSQL(sql);
            }
            else
            {
                wrongNO++;
            }
        }
        
            LoadShowContact();
    }

    private string processMobileNO(string line)
    {
        if (line.Substring(0, 1) == "0") return line;
        else
            if (line.Length == 10) return "0"+line;
            else if (line.Substring(0, 3) == "+88" && line.Length == 14)
            {
                return line.Replace("+88", "");
            }
            else if (line.Substring(0, 2) == "88" && line.Length == 13)
            {
                return line.Replace("88", "");
            }
            else
                return "";
    }
    private string uplaodFile()
    {
        if (fuldStock.HasFile)
        {
            #region fileUpload
            string strPath = string.Empty;
            string dirURL = "tmp";
            if (!Directory.Exists(MapPath(dirURL)))
                Directory.CreateDirectory(MapPath(dirURL));

            getFileNameByReplace fileName = new getFileNameByReplace(fuldStock.FileName, MapPath(dirURL));
            strPath = MapPath(dirURL + "/" + fileName.FileName);
            fuldStock.SaveAs(strPath);
            #endregion

            return fileName.FileName;
        }
        return "";
    }
}